import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time, datetime
from bs4 import BeautifulSoup 
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.action_chains import ActionChains
import requests
from urllib.parse import parse_qs, urlparse
import pandas as pd

chrome_options = Options()
path = Service(r'E:\Software\chromedriver-win64\chromedriver.exe')


# driver = webdriver.Chrome(options=chrome_options, service=path )
driver = webdriver.Chrome()
url = "https://auth.smartedu.cn/uias/login"

driver.get(url)

time.sleep(1)
driver.maximize_window()
actionChains = ActionChains(driver)


auth = {
    'username' : '13726204979',
    'password' : 'aaaa'
}


# input username and password
driver.find_element(By.ID,"username").send_keys(auth.get('username'))

driver.find_element(By.ID,"tmpPassword").send_keys(auth.get('password'))

driver.find_element(By.ID,"agreementCheckbox").click()

time.sleep(10)

# 捕捉滑块元素
slider_input = driver.find_element(By.ID,"slider")
slide_btn = slider_input.find_elements(By.TAG_NAME,"span")[2]

actionChains.drag_and_drop_by_offset(slide_btn, 350, 0).perform()

driver.find_element(By.ID,"loginBtn").click()

time.sleep(5)

# print(driver.page_source)

# 教材链接 
url = 'https://basic.smartedu.cn/tchMaterial'

driver.get(url)
time.sleep(5)

# 左边 列表
# left_list = driver.find_element(By.CLASS_NAME,"index-module_left-nav_2ss3V")


# 小学 初中 小学（五•四学制）初中（五•四学制）  高中 
# 只取 小学 初中 高中 即 index [0, 1, 4]
levels = driver.find_element(By.CLASS_NAME,"index-module_left-nav_2ss3V").find_elements(By.TAG_NAME,"li")

data = []

for index, item in enumerate(levels) :

    # 遍历需要的学段  
    if index in [0,1,4]:  # 只取 小学 初中 高中 即 index [0, 1, 4]
        item.click()
        time.sleep(5)
        print(f'-{item.text}')

        # 学科 版本 年级
        levels_options = driver.find_element(By.CLASS_NAME,"style-module_nav-bar_GVTAR")
        
        # 学科
        subjects = driver.find_element(By.CLASS_NAME,"style-module_nav-bar_GVTAR").find_elements(By.TAG_NAME,"div")[0].find_elements(By.TAG_NAME,"label")
        # 遍历学科 版本 年级
        for _, subject in enumerate(subjects):
            # 处理学科
            print(f'--{subject.text}')
            subject.click()

            time.sleep(5)
            
            # 处理版本  //*[@id="zxxcontent"]/div[4]/div/div[2]/div/div[2]/div[1]/div[2]/div[2]/label[1]
            versions = driver.find_elements(By.XPATH,'//*[@id="zxxcontent"]/div[4]/div/div[2]/div/div[2]/div[1]/div[2]/div[2]/label')
            # print(f'-xxxxxxxxxxxxxx--{len(versions)}')
            # for i in x:
            #     print(f'---{i.text}')
            # versions = driver.find_element(By.CLASS_NAME,"style-module_nav-bar_GVTAR").find_elements(By.TAG_NAME,"div")[1].find_elements(By.TAG_NAME,"label")
            print(f'- len(versions) ::::::--{len(versions)}')
            for _, version in enumerate(versions):
                # 处理版本
                # print(f'---{version.text}')
                if '统编' in version.text or '人教' in version.text or len(versions) == 1:

                    print(f'---{version.text}')
                    version.click()
                    time.sleep(3)

                    # 处理年级 //*[@id="zxxcontent"]/div[4]/div/div[2]/div/div[2]/div[1]/div[3]/div[2]/label[1]
                    grades = driver.find_elements(By.XPATH,'//*[@id="zxxcontent"]/div[4]/div/div[2]/div/div[2]/div[1]/div[3]/div[2]/label')
                    for _, grade in enumerate(grades):
                        # 处理年级
                        grade.click()
                        time.sleep(3)

                        # 处理教材
                        materials = driver.find_elements(By.CLASS_NAME,"index-module_line_LgJAC")
                        for _, material in enumerate(materials):
                            # 处理教材
                            title = material.text
                            # print(f'----{title}')

                            material.click()
                            time.sleep(5)
                            # 切换到第二个tab
                            driver.switch_to.window(driver.window_handles[1])
                            link = driver.current_url
                            # FIXME: 这里需要 出来拿到 link 里面的 contentId
                            # print(f'----{title} : {link}')
                            # 获取URL中的查询字符串部分
                            query_string = urlparse(link).query

                            # 解析查询字符串，得到参数字典
                            parameters = parse_qs(query_string)

                            # 提取参数
                            contentId = parameters.get('contentId', '')[0]
                            # https://r1-ndr.ykt.cbern.com.cn/edu_product/esp/assets/b87e153f-a64c-451a-aa6c-6ed9ac7d6821.pkg/pdf.pdf
                            print(f'----{title} : {contentId}')

                            download = f'https://r1-ndr.ykt.cbern.com.cn/edu_product/esp/assets/{contentId}.pkg/pdf.pdf'

                            data.append({'title':title, 'contentId':contentId, 'link':download})

                            time.sleep(5)

                            # 关闭第二个tab
                            driver.close()
                            # 切换回第一个tab
                            driver.switch_to.window(driver.window_handles[0])





result = pd.DataFrame(data, columns=['title', 'contentId', 'link'])
result.to_csv('smartedu-1.csv', index=False, encoding='utf-8-sig')










